﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;


namespace CCF20Model
{
    partial class Customer : IDataErrorInfo
    {
        
        CCF20Entities ccf20Ent;
        public bool isObjectMaterialzied { get; set; }
        public bool isNewCustomer { get; set; }
        public Customer()
        {
            ccf20Ent = new CCF20Entities();
            this.IsTransfered = false;
            this.CreateDate = DateTime.Now;
            isObjectMaterialzied = false;
            isNewCustomer = false;
        }
        
        
        partial void OnSNChanged()
        {
            this.RemoveError("SN");

            if (string.IsNullOrEmpty(this.SN))
            {
                this.AddError("SN", "SN 卡号不可为空");
            }
            else
            {
                
                var count = from c in ccf20Ent.Customers
                            where c.SN.Equals(this.SN) && c.CustomerID != this.CustomerID
                            select c;


                if (count.Count() > 0)
                    this.AddError("SN", "SN号不可重复");
                
            }
        }

        partial void OnSNPwdChanged()
        {
            this.RemoveError("SNPwd");

            if (string.IsNullOrEmpty(this.SNPwd))
            {
                this.AddError("SNPwd", "VIP 密码不可为空");
            }
        }

        partial void OnIDSChanged()
        {
            this.RemoveError("IDS");

            if (string.IsNullOrEmpty(this.IDS))
            {
                this.AddError("IDS", "IDS 不可为空");
            }
        }



        partial void OnCustomerNoChanged()
        {
            this.RemoveError("CustomerNo");

            if (string.IsNullOrEmpty(this.CustomerNo))
            {
                this.AddError("CustomerNo", "客户号码不可为空");
            }else if (isObjectMaterialzied)
            {
                
                var count = from v in ccf20Ent.Customers
                            where v.CustomerNo.Equals(this.CustomerNo) && v.CustomerID != this.CustomerID
                            select v;
                if (count.Count() > 0)
                    this.AddError("CustomerNo", "客户号码不可重复");

            }
            else if (isNewCustomer)
            {
                var count = from v in ccf20Ent.Customers
                            where v.CustomerNo.Equals(this.CustomerNo)
                            select v;
                if (count.Count() > 0)
                    this.AddError("CustomerNo", "客户号码不可重复");
            }
        }
        partial void OnFirstMobileChanged()
        {
            this.RemoveError("FirstMobile");

            if (string.IsNullOrEmpty(this.FirstMobile))
            {
                this.AddError("FirstMobile", "联系号码不可为空");

            }
        }



        partial void OnNameChanged()
        {
            this.RemoveError("Name");

            if ((this.Name == null) || (this.Name == ""))
            {
                this.AddError("Name", "姓名不可为空");
            }
        }

        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
}
